<template>

    <view>
		<view class="jiangqie-searchbox">
			<view class="jiangqie-search-input">
				<icon type="search" size="18" color="#999"></icon>
				<input
					confirm-type="search"
					placeholder="输入创作者口令搜索壁纸"
					placeholder-class="jiangqie-input-plholder"
					class="jiangqie-input"
					@confirm="handerSearchConfirm"
					@input="handlerSearchInput"
				/>
				<text class="cuIcon-pullright text-blue" style="font-size: 20px;" @click="handerSearch"></text>
			</view>
		</view>

        <view>

            <view class="tab-content">
                <block v-if="currentTab == 0">
                    <!--banner begin  :class="isMpKuaishou ? 'platform-ks' : ''"-->
                    <view v-if="banner" class="tui-banner-box" 
					>
						
                        <swiper
                            :indicator-dots="true"
                            :autoplay="true"
                            interval="3000"
                            duration="150"
                            class="tui-banner-swiper"
                            :circular="true"
                            previous-margin="15rpx"
                            next-margin="15rpx"
                            @change="handlerSlideChange"
                        >
                            <block v-for="(data,index) in banner" :key="index">
                                <swiper-item class="tui-banner-item" :data-link="data.link" :data-jumpappid="data.jumpAppid" @click.stop="handlerActiveClick"
								>
                                    <image
                                        :src="data.image"
                                        :class="'tui-slide-image ' + (current == index || (current == undefined && index == 0) ? '' : 'tui-banner-scale')"
                                        mode="aspectFill"
                                        :lazy-load="true"
                                    />
                                </swiper-item>
                            </block>
                        </swiper>
                    </view>
                    <!--banner end-->
                    <view class="container">
                        <!--公告~toutiao begin-->
                        <view class="jiangqie-rolling-news">
                            <image src="@/static/images/toutiao.png"></image>
                            <text class="sw-line">|</text>
                            <swiper :vertical="true" :autoplay="true" :circular="true" interval="3000" class="jiangqie-swiper">
                                <block v-for="(item,index) in notice" :key="index">
                                    <swiper-item class="jiangqie-swiper-item">
                                        <view class="jiangqie-news-item" :data-link="item.link" :data-id="item.id" @click="handlerNoticeClick">
                                           <!-- <text>{{ item.name }}</text> -->
                                            {{ item.title }}
                                        </view>
                                    </swiper-item>
                                </block>
                            </swiper>
                        </view>

                        <!--toutiao end-->

                        <!-- 达人模块  begin-->

                        <view class="jiangqie-news-view">
                            <view v-if="talents && talents.length > 0" class="jiangqie-news-type">
                                <text>合作达人</text>
                            </view>
                        </view>

                        <view v-if="talents && talents.length > 0" class="guoshao-news-icon">
                            <view v-for="(item,index) in talents" :key="index" class="guoshao-news-iconbox" :data-userid="item.userid" @click.stop.prevent="talentClick">
                                <view class="">
                                    <image class="jiangqie-news-iconimg" :src="item.avatar_url  == null ? '../../static/images/user.png' : item.avatar_url"></image>
                                </view>
                            </view>
                        </view>

                        <!-- 达人模块  end-->

                        <!--图标模块 可单独设置背景色、背景图、图标图 begin-->
                        <view v-if="iconNav && iconNav.length > 0" class="jiangqie-news-icon">
                            <view
                               v-for="(item,index) in iconNav" :key="index"
                                class="jiangqie-news-iconbox"
                                :data-link="item.link"
                                :data-jumpappid="item.jumpAppid"
                                @click.stop.prevent="handlerIconNavClick"
                            >
                                <view class="jiangqie-news-iconbg">
                                    <image class="jiangqie-news-iconimg" :src="item.image"></image>
                                </view>
                                <text>{{ item.title }}</text>
                            </view>
                        </view>

                        <!--图标模块 可单独设置背景色、背景图、图标图 end-->

                        
                        <!-- 壁纸列表 begin -->
                        <view class="jiangqie-news-view">
                            <view v-if="postsLast && postsLast.length > 0" class="jiangqie-news-type">
                             
                                <text>精选壁纸</text>
                            </view>
                        </view>

                        <view class="list-box data-v-0034d6f4">
                            <block  v-for="(item,index) in postsLast" :key="index">
                                <block v-if="item.classType == 1">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="image data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="item.classType == 2">
                                
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="image data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="item.classType == 3">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="background-image data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="item.classType == 4">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="emoticon data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="item.classType == 5">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="emoticon data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                          
                            </block>
                        </view>
                    </view>
                    <!-- 壁纸列表 end -->
                    <!--加载loadding-->
                    <tui-loadmore :visible="loaddingLast"></tui-loadmore>
                    <tui-nomore :visible="!pullUpOnLast"></tui-nomore>
                    <!--加载loadding-->
                </block>
				
                <block v-else>
                    <view class="container">
                        <view class="jiangqie-news-view">
                            
                        </view>
                        <!-- 导航分类 -->
                        <view class="list-box data-v-0034d6f4">
                            <block v-for="(item,index) in posts" :key="index" >
                                <block v-if="classType == 1">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="image data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="classType == 2">
                                
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="image data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="classType == 3">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="background-image data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="classType == 4">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="emoticon data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                                <block v-if="classType == 5">
                                    <image
                                        :src="item.thumbnail"
                                        mode="aspectFill"
                                        class="emoticon data-v-0034d6f4"
                                        :index="index"
                                        :data-id="item.id"
                                        @click.stop.prevent="handlerArticleClick"
                                    ></image>
                                </block>
                            </block>
                        </view>
                        <!-- 导航分类end -->
                    </view>

                    <!--加载loadding-->
                    <tui-loadmore :visible="loadding"></tui-loadmore>
                    <tui-nomore :visible="!pullUpOn"></tui-nomore>
                    <!--加载loadding-->
                </block>
            </view>
        </view>
        <image src="@/static/images/huojian.png" class="goTop" v-if="floorstatus" @click="goTop"></image>
        
		<!--
		<view class="kefu" @click="kefu">
			<image src="../../static/images/kefu.png" class=""></image>
			<text>创作者入住</text>
		</view>
		-->
		<!--加载框 start-->
        <tui-loading :visible="loading"></tui-loading>
        <!--加载框 end-->
    </view>
</template>

<script>
import tuiLoading from '../../components/loading/loading';
import tuiLoadmore from '../../components/loadmore/loadmore';
import tuiNomore from '../../components/nomore/nomore';
/*
 * Author: 郭少
 * github: https://github.com/guoshao99/wallpaper-applet
 * gitee: https://gitee.com/guoshao99/wallpaper-applet
 * QQ：2549789059
 * 微信：2549789059
 */

const authentication = require('../../utils/authentication');

const Constants = require('../../utils/constants');

const Api = require('../../utils/api.js');

const Rest = require('../../utils/rest');

const util = require('../../utils/util.js');

const http = require('../../utils/http.js');

const platformEnv = require('../../utils/platformEnv.js');

let setinad = null;

export default {
    components: {
        tuiLoading,
        tuiLoadmore,
        tuiNomore
    },
    data() {
        return {
			isMpKuaishou:platformEnv.isMpKuaishou,
            pageNum: 1,

            //首页page
            cpageNum: 1,

            //分类page
            scrollTop: 0,

            //分类类型
            classType: 0,

            //当前首页滚动条
            cardCur: 0,

            background: '',
            floorstatus: false,
            banner: [],
            notice: [],
            currentTab: 0,

            //预设当前项的值
            //幻灯片
            slide: [],

            //图片导航
            iconNav: [],

            //热门文章
            hot: [],

            //热门tab
            postsLast: [],

            loaddingLast: false,
            pullUpOnLast: true,

            //其他tab
            posts: [],

            loadding: false,
            pullUpOn: true,

            pagead: 1,
            pageadB: 0,
            pageadC: 0,
            thisPage: 'index',
            talents: [],
            canSearch: '',
            keyword: '',
            current: {},

            data: {
                link: '',
                jumpAppid: '',
                image: ''
            },

            setAD: {
                banner: '',
                videoid: '',
                gridid: '',
                customAid: '',
                customBid: '',
                customCid: ''
            },

            loading: false
        };
    },
    onLoad: function (options) {
        var that = this;
        var params = {
            url: '/platform/index',
            method: 'GET',
            data: {},
            callBack: function (res) {
                that.setData({
                    notice: res.data.notices,
                    banner: res.data.banners,
                    //iconNav: res.data.iconNav,
                    hot: res.data.hot,
                    talents: res.data.talents
                });
            }
        };
        http.request(params);
        that.init();
		
		util.getAD(that, function () {
			that.setInterstitialAd(); //加载插屏广告
		});
    },
    onPullDownRefresh: function () {
        let that = this;
        that.pageNum = 1; //加载文章

        this.init(); //当处理完数据刷新后,停止当前页面的下拉刷新。

        uni.stopPullDownRefresh();
    },
    onReachBottom: function () {
        if (this.currentTab == 0) {
            if (!this.pullUpOnLast) {
                return;
            }

            this.loadPostLast(false);
        } else {
            if (!this.pullUpOn) {
                return;
            }

            this.loadPost(false);
        }
    },
    onShow() {
       
        var that = this;

    },
    onHide() {
        clearInterval(setinad);
    },
	onShareAppMessage: function() {
		uni.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		});
		return {
			title: '免费下载高清壁纸,头像,表情包',
			path: '/pages/index/index',
		};
	},
   
    //转发朋友圈
    onShareTimeline: function () {
        var that = this;
        return {
            title: that.shares.share_title,
            imageUrl: that.shares.share_image
        };
    },
    onPageScroll: function (e) {
        // 获取滚动条当前位置
        if (this.currentTab == 0) {
            this.scrollTop = e.scrollTop;
        }

        if (e.scrollTop > 200) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },
    methods: {
        init: function () {
            //加载首页图片
            this.loadPostLast(true);
        },

        // 获取小程序插屏广告
        setInterstitialAd: function () {
            var that = this;
			console.logthat.setAD.interstitialid
            if (that.setAD.interstitialid && uni.createInterstitialAd) {
                let interstitialAd = uni.createInterstitialAd({
                    adUnitId: that.setAD.interstitialid
                }); // 监听插屏错误事件

                interstitialAd.onError((err) => {
                    console.error(err);
                }); // 显示广告

				  if (interstitialAd) {
					if (that.setAD.switch_inad == 'yes') {
					  setinad = setInterval(() => {
						interstitialAd.show().catch(err => {
						  console.error(err);
						});
					  }, 2000);
					} else {
					  setTimeout(() => {
						interstitialAd.show().catch(err => {
						  console.error(err);
						});
					  }, 6000);
					}
				  }
            }
        },

        //输入
        handlerSearchInput: function (e) {
            this.keyword = e.detail.value;
            this.setData({
                canSearch: this.keyword.length > 0,
                keyword: e.detail.value
            });
        },

        handerSearchConfirm: function (e) {
			if(this.keyword == ''){
				uni.showToast({
					title:'请输入创作者口令',
					icon:'none',
				})
				return;
			}
			
			this.getTalentInfo();
			
            // uni.navigateTo({
            //     url: '/pages/search/search?keyword=' + this.keyword
            // });
        },
		handerSearch:function(e){
			if(this.keyword == ''){
				uni.showToast({
					title:'请输入创作者口令',
					icon:'none',
				})
				return;
			}
			
			this.getTalentInfo();
			
			// uni.navigateTo({
			//     url: '/pages/search/search?keyword=' + this.keyword
			// });
		},


        //nav end ----
        //slide start----
        handlerSlideChange: function (e) {
            this.setData({
                current: e.detail.current
            });
        },

        //slide end----
        //tab -- start
        swichNav: function (e) {
            let cur = e.currentTarget.dataset.current;
            let classType = e.currentTarget.dataset.classtype;

            if (this.currentTab == cur) {
                return false;
            }

            this.setData({
                currentTab: cur,
                classType: classType,
                cpageNum: 1
            }); //分类数据

            this.loadPost(true); //如果点击首页进行定位

            uni.pageScrollTo({
                scrollTop: cur == 0 ? this.scrollTop : 0
            });
        },

        handlerTabMoreClick: function (e) {
            uni.switchTab({
                url: '/pages/categories/categories'
            });
        },

        //tab -- end
        handlerNoticeClick: function (e) {
            let link = e.currentTarget.dataset.link;
            let id = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: link + '?id=' + id
            });
        },

        handlerIconNavClick: function (e) {
            let link = e.currentTarget.dataset.link;
            let jumpAppid = e.currentTarget.dataset.jumpappid;
            this.openLink(link, jumpAppid);
        },

        handlerActiveClick: function (e) {
			console.log(e)
            let link = e.currentTarget.dataset.link;
            let jumpAppid = e.currentTarget.dataset.jumpappid;
            this.openLink(link, jumpAppid);
        },

        handlerArticleClick: function (e) {
            let post_id = e.currentTarget.dataset.id;
            uni.navigateTo({
                url: '/pages/article/article?post_id=' + post_id+"&flag=1"
            });
        },

        //加载数据
        loadPost: function (refresh) {
            let that = this;

            if (!refresh) {
                that.cpageNum++;
                that.setData({
                    loadding: true
                });
            } //加载分类图片  begin

            var params = {
                url: '/platform/wallpaper',
                method: 'GET',
                data: {
                    pageNum: that.cpageNum,
                    pageSize: Constants.JQ_PER_PAGE_COUNT,
                    order: -1,
                    isSys: 1
                },
                callBack: function (res) {
                    that.setData({
                        loadding: false,
                        posts: refresh ? res.data.list : that.posts.concat(res.data.list),
                        pullUpOn: res.data.isLastPage ? false : res.data.isLastPage
                    });
					
                }
            };
            http.request(params); // 加载分类 end
        },

        //加载数据
        loadPostLast: function (refresh) {
            let that = this;

            if (!refresh) {
                that.pageNum++;
                that.setData({
                    loaddingLast: true
                });
            } //加载分类图片  begin

            var params = {
                url: '/platform/wallpaper',
                method: 'GET',
                data: {
                    pageNum: that.pageNum,
                    pageSize: Constants.JQ_PER_PAGE_COUNT,
                    order: -1,
                    isSys: 1
                },
                callBack: function (res) {
                    that.setData({
                        loaddingLast: false,
                        postsLast: refresh ? res.data.list : that.postsLast.concat(res.data.list),
                        pullUpOnLast: !res.data.isLastPage ? true : false
                    });
                }
            };
            http.request(params); // 加载分类 end
        },

        // cardSwiper
        cardSwiper(e) {
            this.setData({
                cardCur: e.detail.current
            });
        },

        openLink: function (link, jumpAppid) {
            if (jumpAppid == '' || jumpAppid == undefined) {
                if (link.startsWith('/pages')) {
                    uni.navigateTo({
                        url: link
                    });
                }
            } else {
                if (link.startsWith('/pages')) {
                    uni.navigateToMiniProgram({
                        appId: jumpAppid,
                        //appid
                        path: link,
                        //path
                        extraData: {
                            //参数
                        },
                        envVersion: 'release',

                        //开发版develop 开发版 trial   体验版 release 正式版
                        success(res) {
                            console.log('跳转成功'); // 打开成功
                        }
                    });
                }
            }
        },

        goTop: function (e) {
            // 一键回到顶部
            if (uni.pageScrollTo) {
                uni.pageScrollTo({
                    scrollTop: 0,
					duration: 500
                });
            } else {
                uni.showModal({
                    title: '提示',
                    content: '当前版本过低，无法使用该功能，请升级到最新版本后重试。'
                });
            }
        },

        talentClick: function (e) {
            let userid = e.currentTarget.dataset.userid;

            if (userid == 0) {
                uni.navigateTo({
                    url: '/pages/talent/talent?userid=' + userid
                });
            } else {
                userid = userid.replace(/\s+/g, ''); //全局替换空格

                uni.navigateTo({
                    url: '/pages/talent/mytalent/mytalent?userid=' + userid
                });
            }
        },
		kefu:function(){
			
			if(authentication.isLogin()){
				uni.navigateTo({
				    url: '/pages/apply/apply'
				});
			}else{
				uni.showModal({
					title:'提示',
					content:'创作者入住需要绑定信息，请先登录',
					success(res) {
						if(res.confirm){
							uni.switchTab({
							    url: '/pages/ucenter/ucenter'
							});
						}
					}
				})
			}
			
			// tt.openAwemeUserProfile({
			//   success: (res) => {
			//     console.log(res);
			//   },
			//   fail: function () {
			//       console.log('--- 转发失败 ---');
			//   }
			// });
		},
		getTalentInfo: function () {
		    let that = this;
		    
		    uni.showLoading({
		        title: '加载中' // 内容
		    });
		    var params = {
		        url: '/talent/getTalentByWord',
		        method: 'GET',
		        data: {
		            word: that.keyword
		        },
		        callBack: function (res) {
					uni.hideLoading();
		            if (res.code == 200) {
						console.log(res.data);
		                uni.navigateTo({
		                    url: '/pages/talent/mytalent/mytalent?userid=' + res.data.userid
		                });
		            } else {
		                uni.navigateTo({
		                    url: '/pages/search/search?keyword=' + that.keyword
		                });
		            }
		            
		        }
		    };
		    http.request(params);
		},
    }
};
</script>
<style>
@import './index.css';
</style>
